import { Injectable } from '@angular/core';
import { AppNavigationService } from '@app/shared/layout/nav/app-navigation.service';
import { BreadcrumbItem } from '@app/shared/layout/nav/breadcrumb-item';
import { AppMenuFlatItem } from './app-menu-flat-item';


@Injectable()
export class AppMenuService {

    //查询栏目名
    getMenuName(kind: number): string {
        let item = this.getMenuInfoByKind(kind);
        if (item !== null) {
            return item.displayName;
        }
        return "";
    }
    getMenuGuid(kind: number) {
        let item = this.getMenuInfoByKind(kind);
        if (item !== null) {
            return item.guid;
        }
        return "";
    }
    getMenuInfoByKind(kind: number): AppMenuFlatItem {
        let list = this.getMenuPath(kind);
        if (list && list.length > 0) {
            return list[list.length - 1];
        }
        return null;
    }

    //查询从一级栏目到当前栏目的数据
    getMenuPath(kind: number): AppMenuFlatItem[]  {
        let menuData = this.getContentMenuData();
        let idList: AppMenuFlatItem[] = [];
        if (this.getMenuTrace(idList, menuData.items, kind)) {
            return idList;
        } else {
            return [];
        }
    }

    getBreadcrumbsForMenu(kind: number): BreadcrumbItem[] {
        let path = this.getMenuPath(kind);
        return path.map(item => {
            //if (kind === item.kind) {
           //     return new BreadcrumbItem(item.displayName, '/app/main/' + item.url + '/' + item.kind.toString());
            //} else {
                return new BreadcrumbItem(item.displayName);
            //}
        });
    }

    // 获取该栏目的所有子栏目
    getAllSonsBykind(kind: number, predicate: { (item: abp.nav2.IMtBackEndUserMenuItem): boolean }): AppMenuFlatItem[] {
        let sons: AppMenuFlatItem[] = [];

        let path = this.getMenuPath(kind);
        if (path.length > 0) {
            var item = this.getLastFrontendMenuItem(path);
            if (item != null) {
                for (let son of item.items) {
                    if (predicate) {
                        if (predicate(son)) {
                            sons.push(this.translate(son));
                        }
                    }
                    else {
                        sons.push(this.translate(son));
                    }
                }
            }
        }
        return sons;
    }

    // 获取该栏目的所有同级栏目
    getAllBrothersBykind(kind: number, includeSelf: boolean, predicate: { (item: abp.nav2.IMtBackEndUserMenuItem): boolean }): AppMenuFlatItem[] {
        let sons: AppMenuFlatItem[] = [];

        let path = this.getMenuPath(kind);
        if (path.length > 1) {
            path.length = path.length - 1;

            let item = this.getLastFrontendMenuItem(path);
            if (item != null) {
                for (let son of item.items) {
                    if (son.auxiliaryId === kind && includeSelf === false) {
                        continue;
                    }

                    if (predicate) {
                        if (predicate(son)) {
                            sons.push(this.translate(son));
                        }
                    }
                    else {
                        sons.push(this.translate(son));
                    }
                }
            }
        }
        return sons;
    }


    private getLastFrontendMenuItem(path: AppMenuFlatItem[]) {
        let items = this.getContentMenuData().items;

        for (let i = 0; i < path.length; i++) {
            let item = this.getFrontendMenuItemDefinitionFromList(items, path[i].guid);
            if (item == null) {
                //throw new Error('栏目未找到.');
                return null;
            }

            if (i == path.length - 1) {
                return item;
            }
            else {
                items = item.items;
            }
        }

        //异常，栏目未找到
        return null;
    }

    private getFrontendMenuItemDefinitionFromList(subItems: abp.nav2.IMtBackEndUserMenuItem[], guid: string) {
        for (let item of subItems) {
            if (item.name == guid) {
                return item;
            }
        }

        return null;
    }

    private getContentMenuData(): abp.nav2.IMtBackEndUserMenu {
        return abp.nav2.menus[AppNavigationService.ContentMenuName];
    }

    private getMenuTrace(idList: AppMenuFlatItem[], subItems: abp.nav2.IMtBackEndUserMenuItem[], kind: number) {
        for (let i = 0; i < subItems.length; i++) {
            let item = subItems[i];
            idList.push(this.translate(item));
            if (kind == item.auxiliaryId) {
                return true;
            }
            if (this.getMenuTrace(idList, item.items, kind)) {
                return true;
            }
            idList.length = idList.length - 1;
        }
    }

    private translate(item: abp.nav2.IMtBackEndUserMenuItem): AppMenuFlatItem {
        return {
            "guid": item.name,
            "order": item.order,
            "kind": item.auxiliaryId,
            "displayName": item.displayName,
            "icon": item.icon,
            "url": item.url,
            "target": item.target,
            "headerInfo": item.headerInfo,
            "frontEndPageId": item.frontEndPageId,
            "backEndModuleId": item.backEndModuleId
        };
    }
}
